package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Car;
import com.qf.exception.OAException;
import com.qf.mapper.CarMapper;
import com.qf.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class CarServiceImpl implements CarService {

    @Resource
    private CarMapper carMapper;


    @Override
    public PageInfo<Car> findAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Car> list = carMapper.selectAll();
        PageInfo<Car> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Car> findAll() {
        List<Car> list = carMapper.selectAll();

        return list;
    }

    @Override
    public Car findById(Integer id) {
        Car car = carMapper.selectByPrimaryKey(id);
        return car;
    }

    @Override
    @Transactional
    public void updateCar(Car car) {
        int count = carMapper.updateByPrimaryKeySelective(car);
        if (count!=1){
            log.info("【车辆信息】修改车辆失败，seal={}",car);
            throw new OAException(199,"修改车辆失败");
        }
    }

    @Override
    @Transactional
    public void delCar(Integer id) {
        int count = carMapper.deleteByPrimaryKey(id);
        if (count!=1){
            log.info("【车辆信息】删除车辆失败，id={}",id);
            throw new OAException(199,"删除车辆失败");
        }
    }

    @Override
    @Transactional
    public void delAllCar(Integer[] ids) {
        int count=0;
        for (Integer id : ids) {
            carMapper.deleteByPrimaryKey(id);
            count++;
        }
        if (count!=ids.length){
            log.info("【车辆信息】删除车辆失败，ids={}", Arrays.toString(ids));
            throw new OAException(199,"删除车辆失败");
        }
    }

    @Override
    public  List<Car> selectCar(Integer[] ids) {
        List<Car> list=new ArrayList<>();
        for (Integer id : ids) {
            Car car = carMapper.selectByPrimaryKey(id);
            if (car==null){
                log.info("【车辆信息】导出失败，ids={}", Arrays.toString(ids));
                throw new OAException(199,"导出车辆信息失败");
            }
            list.add(car);
        }
        return list;
    }
}
